package org.nimi317.web_gis.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import jakarta.validation.Valid;
import org.nimi317.web_gis.annotation.Page;
import org.nimi317.web_gis.entity.QualityModel;
import org.nimi317.web_gis.entity.R;
import org.nimi317.web_gis.form.post.QualityModelAddPost;
import org.nimi317.web_gis.form.post.QualityModelPost;
import org.nimi317.web_gis.model.AirQualityModel;
import org.nimi317.web_gis.service.QualityModelService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * (QualityModel)表控制层
 *
 * @author makejava
 * @since 2024-05-23 08:42:27
 */
@RestController
@RequestMapping("qualityModel")
@SaCheckLogin
@SaCheckRole("admin")
public class QualityModelController {

    /**
     * 服务对象
     */
    private final QualityModelService qualityModelService;

    private final AirQualityModel qualityModel;

    public QualityModelController(QualityModelService qualityModelService, AirQualityModel qualityModel) {
        this.qualityModelService = qualityModelService;
        this.qualityModel = qualityModel;
    }

    /**
     * 分页查询
     */
    @PostMapping("page")
    @Page
    public R<?> page(@RequestBody @Valid QualityModelPost post) {
        return R.ok(qualityModelService.pageData(post));
    }

    @PostMapping("associate")
    public R<?> add(@RequestBody @Valid QualityModelAddPost post) {
        QualityModel model = new QualityModel();
        BeanUtils.copyProperties(post, model);
        qualityModelService.save(model);
        qualityModel.addModel(model);
        return R.ok("链接成功");
    }

    @DeleteMapping("{id}")
    public R<?> delete(@PathVariable String id) {
        if (qualityModelService.removeById(id)) {
            qualityModel.removeModel(id);
            return R.ok("解除关联成功");
        }
        return R.fail("解除关联失败");
    }
}

